#include <graphics.h>
#include <stdio.h>

int flag=0;
typedef int Init_Map[25][37];
Init_Map map;
char name[20]="map1";

void Draw_Rec(int x,int y,int color)
{
    setcolor(color);
    setfillstyle(9,color);
    rectangle(x-7,y-7,x+7,y+7);
    floodfill(x,y,color);
}

void Edit_Cell()
{
    int i,j,adj;
    char input;
    again:
    printf("Input the position and the authority of the cell(i,j,adj):");
    scanf("%d%d%d",&i,&j,&adj);
    if(i>=25||j>=37||i<0||j<0) 
    {
        printf("Illegal input form, press any continue, or [ESC] to cancel...\n");
        input=getch();
        if (input==27) return;
        else
            goto again;
    }
    map[i][j]=adj;
    flag=1;    
}

void Edit_Lines()
{
    int i,j,adj,low1,high1,low2,high2;
    char input;
    again:
    printf("Input the upper and lower limit of the lines: ");
    scanf("%d%d",&low1,&high1);
    if(low1>=25||low1<0||high1>=25||high1<0) 
    {
        printf("Illegal input form, press any continue, or [ESC] to cancel...\n");
        input=getch();
        if (input==27) return;
        else
            goto again;
    }
    printf("Input the upper and lower limit of the columns: ");
    scanf("%d%d",&low2,&high2);
        if(low2>=37||low2<0||high2>=37||high2<0) 
    {
        printf("Illegal input form, press any continue, or [ESC] to cancel...\n");
        input=getch();
        if (input==27) return;
        else
            goto again;
    }
    printf("Input authority of the cells:");
    scanf("%d",&adj);
    for(i=low1;i<=high1;i++)
        for(j=low2;j<=high2;j++)
            map[i][j]=adj;
    flag=1;
}    
            
void Replace_Cells()
{
    int i,j,adj_old,adj_new;
    printf("Which kind of the cell do you want to replace? ");
    scanf("%d",&adj_old);
    for(i=0;i<25;i++)
        for(j=0;j<37;j++)
            if(map[i][j]==adj_old)
            {
                printf("This kind of cells does exist, please input the new authority: ");
                scanf("%d",&adj_new);
                goto end;
            }
    end:
    if(i<25) 
    {
        for(i=0;i<25;i++)
            for(j=0;j<37;j++)
                if(map[i][j]==adj_old) map[i][j]=adj_new;
        flag=1;
    }    
    else
    {
        printf("This kind of cells doesn't exist!\n");
        getch();
    }    
}
    
void Creat_Map()
{
    int i,j;
    char temp_name[20],input;
    strcpy(temp_name,name);
    again:
    printf("Please input a name for the new map:");
    scanf("%s",name);
    if(fopen(name,"rb")==NULL)
    {
        for(i=0;i<25;i++)
            for(j=0;j<37;j++)
                map[i][j]=0;
        flag=1;
    }
    else 
    {
        strcpy(name,temp_name);
        printf("The Filename has existed, Press any key to try again or [ESC] to cancel...\n");
        input=getch();
        if(input==27) return;
        goto again;
    }    
}    

void Save_Map()
{
    FILE *fp;
    char name_[20];
    strcpy(name_,name);
    if((fp=fopen(name,"wb"))==NULL) exit(0);
    fwrite(map,1,sizeof(Init_Map),fp);
    fclose(fp);
}    

void Read_Map()
{
    FILE *fp;
    char input,temp_name[20];
    strcpy(temp_name,name);
    again:
    printf("Please input the name of the map: ");
    scanf("%s",name);
    if((fp=fopen(name,"rb"))==NULL)
    {
        printf("Opening map failed, press any key to try again or [ESC] to cancel...\n");
        input=getch(); 
        if(input==27) 
        {
            strcpy(name,temp_name);
            return;
        }    
        goto again;
    }
    fread(map,1,sizeof(Init_Map),fp);
    fclose(fp);
}    

void Print_Map()
{
    int i,j;
    system("cls");
    printf("   ");
    for(i=0;i<37;i++)
        printf("%2d",i);
    printf("\n");
    for(i=0;i<25;i++)
    {
        printf("%2d:",i);
        for(j=0;j<37;j++)
            switch(map[i][j])
            {
                case 0:printf("##");break;
                case 1:printf("  ");break;
                case 2:printf("**");break;
                case 3:printf("$$");break;
            }    
        printf("\n");
    }
    printf("\n");   
}    

void Initgra()
{
    int gd=DETECT,gm;
    initgraph(&gd,&gm,"");
}

void Preview()
{
    int i,j;
    Initgra();
    for(i=0;i<25;i++)
        for(j=0;j<37;j++)
            switch(map[i][j])
            {
                case 0:Draw_Rec(50+15*j,50+i*15,BROWN);break;
                case 1:Draw_Rec(50+15*j,50+i*15,GREEN);break;
                case 2:Draw_Rec(50+15*j,50+i*15,LIGHTGRAY);break;
                case 3:Draw_Rec(50+15*j,50+i*15,RED);break;
            }
    getch();
    closegraph();
    system("mode con: cols=140 lines=130");
}

void Control()
{
    FILE *fp;
    char input;
    int i,j;
    printf("Before edit, please load a map first.\n");
    again:
    printf("Please input the name of the map: ");
    scanf("%s",name);
    if((fp=fopen(name,"rb"))==NULL)
    {
        printf("Opening map failed, press any key to try again or ESC to exit...\n");
        input=getch(); 
        if(input==27) exit(0);    
        goto again;
    }
    fread(map,1,sizeof(Init_Map),fp);
    fclose(fp);
    again2:
    Print_Map();
    printf("Edit one cell to press.............1\n");
    printf("Edit lines or columns to press.....2\n");
    printf("Replace Cells to press ............3\n");
    printf("Creat new map to press.............4\n");
    printf("Reload map to press................5\n");
    printf("Exit to press......................6\n");
    printf("Preview to Press...................7\n");
    again1:
    input=getch();
    switch(input)
    {
        case'1':Edit_Cell();break;
        case'2':Edit_Lines();break;
        case'3':Replace_Cells();break;
        case'4':if(flag==1)
                {
                    printf("The information of the map have been changed, do you want to save it?(Y/N)\n");
                    again5:
                    input=getch();
                    if(input=='y'||input=='Y')
                    {
                        Save_Map();
                        flag=0;
                        Creat_Map();
                    }    
                    else if(input=='n'||input=='N') 
                    {
                        flag=0;
                        Creat_Map();
                    }    
                    else goto again5;
                }
                else Creat_Map();
                break;
        case'5':if(flag==1)
                {
                    printf("The information of the map have been changed, do you want to save it?(Y/N)\n");
                    again3:
                    input=getch();
                    if(input=='y'||input=='Y')
                    {
                        Save_Map();
                        flag=0;
                        Read_Map();
                    }    
                    else if(input=='n'||input=='N') 
                    {
                        Read_Map();
                        flag=0;
                    }    
                    else goto again3;
                }
                else Read_Map();
                break;
        case'6':if(flag==1)
                {
                    printf("The information of the map have been changed, do you want to save it?(Y/N)\n");
                    again4:
                    input=getch();
                    if(input=='y'||input=='Y')
                    {
                        Save_Map();
                        flag=0;
                        exit(0);
                    }    
                    else if(input=='n'||input=='N')
                    {
                        flag=0;
                        exit(0);
                    }    
                    else goto again4;
                }
                else exit(0);   
        case'7':Preview();break;
        default:goto again1;
    }
    goto again2;          
}

void Menu()
{
    system("mode con: cols=140 lines=130");
    printf("           **********************************************************\n");
    printf("           *                                                        *\n");
    printf("           *                RPG      MAP      EDITER                *\n");
    printf("           *                                                        *\n");
    printf("           **********************************************************\n");
    printf("\n\n                                                           Made by LK\n");
    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n                        Press any key to continue...\n");
    getch();
    system("cls");
}    

main()
{
    Initgra();
    closegraph();
    Menu();
    Control();
}           
    

